package com.gitee.cui.springcloud.order.service.impl;

import javax.annotation.Resource;

import com.gitee.cui.springcloud.order.dao.OrderDao;
import com.gitee.cui.springcloud.order.domain.Order;
import com.gitee.cui.springcloud.order.service.AccountService;
import com.gitee.cui.springcloud.order.service.OrderService;
import com.gitee.cui.springcloud.order.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-03-26 12:15
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

	@Resource
	private OrderDao orderDao;

	@Resource
	private StorageService storageService;

	@Resource
	private AccountService accountService;

	@Override
	@GlobalTransactional(name = "sfp_order_rollback", rollbackFor = Exception.class)
	public void create(Order order) {
		log.info("-------------->开始创建订单操作");
		// 创建订单
		orderDao.create(order);

		// 扣库存
		log.info("------------->开始扣除库存，productId: {}, count: {}", order.getProductId(), order.getCount());
		storageService.decrease(order.getProductId(), order.getCount());
		log.info("-------------->扣库存操作end");

		// 扣账户金额
		log.info("-------------->开始扣除账户金额，userId: {}, money: {}", order.getUserId(), order.getMoney());
		accountService.decrease(order.getUserId(), order.getMoney());
		log.info("-------------->开始扣除账户金额end");

		// 更新订单状态
		log.info("-------------->开始更新订单状态");
		orderDao.update(order.getId(), 0);
		log.info("-------------->更新订单状态end");
	}
}
